<?php
/*==========================================================================*\
| AGRIVIET - CONTENT MANAGERMENT SYSTEM						
\*==========================================================================*/


	$pathdb = "uploads/cats"; // Duong dan ghi vao database		
	$imagepath="../uploads/cats/"; // Duong dan de upload Image
	$upload_allow_extension = "jpg,png,gif"; // Cac phan mo rong duoc cho phep

if (isset($_POST['butSave'])) {
	$title = trim($_POST['title']);
	//$links = trim($_POST['links']);
	$alias = vietdecode($title);
	$parent = $_POST['parent'];
//	$section=$_POST['section'];
	$published = $_POST['published'];
	$ordering = $_POST['ordering'];	
	$date_add = mktime();#$_POST['date_add'];
	
	$err="";	
	
	if ($err==''){
		/* Cap nhat hoac them moi */
		if (!empty($_POST['id'])) {
			$oldid = $_POST['id'];
			$sql = "update product_categories set title='".$title."',alias='".$alias."',parent_id='".$parent."',";
			$sql.= "published='".$published."',ordering='".$ordering."', date_add='".$date_add."' where id='".$oldid."'";
		}else {
			// Insert data
			$sql = "insert into product_categories (title,alias,parent_id,published,ordering,date_add)";
			$sql.= "values ('".$title."','".$alias."','".$parent."','".$published."','".$ordering."','".$date_add."')";
		}
		/* EOF - Cap nhat hoac them moi */
		if(mysql_query($sql)){ // Neu thuc hien query thanh cong
		
			if(empty($_POST['id'])) $oldid = mysql_insert_id();
			
			
			
		//Upload image here
		$filename="";
		if(is_uploaded_file($_FILES['img']['tmp_name']))
		{
			//Check file extension
			if (check_upload_extension($_FILES['img']['name'],$upload_allow_extension) == 1){
			$filename = generate_name('categories',$oldid,$_FILES['img']['name']);
			move_uploaded_file($_FILES['img']['tmp_name'],$imagepath . $filename);
			resize_image($imagepath.$filename,$imagepath.$filename,80,80,95);			

			$_POST["img"] = $filename;
					
			$sqlUpdateField = " image ='$pathdb/".$filename."'";
			
				if($sqlUpdateField!='')
				{
					$sqlUpdate = "update product_categories set $sqlUpdateField where id='".$oldid."'";
					mysql_query($sqlUpdate);
				}
			}
		} else {
			$_POST["img"] = "";
		}
		// Eof Upload image
		} // Neu thuc hien Query thanh cong
		else {
			$err =  "Không th&#7875; c&#7853;p nh&#7853;t";
		}
		
	} // if ($err=='')
	
	if ($err==''){
	// Top Menu PRODUCT
	$sql = 'SELECT * FROM product_categories WHERE  published=1 AND parent_id=0 order by ordering ASC';
	$query = mysql_query($sql);
	while ( $row = mysql_fetch_object($query) )
	{
	   $menu_array[$row->id] = array('id' => $row->id,
									 'name' => $row->title,
									 'alias' => $row->alias,
									'section' => $row->section,
									 'links' => $row->links,
									 'parent' => $row->parent_id
									 );
	}
	//$tpl->assign('topmenu',generate_menu(0));
	echo create_topmenu(generate_menu(0,"product"),'../html/productmenu.htm');
	echo '<script>window.location="index.php?act=product_cat&code=1"</script>';
	}
  	else echo "<p align=center class='red'>".$err."</p>";
}


action('Category');?>	
<?

	 if (isset($_GET['id'])){
	 $result=$db->query("SELECT * FROM product_categories  WHERE id=$id LIMIT 1");
	 $row=$db->fetchNextObject($result);
	 $title=$row->title;
	// $links=$row->links;
	// $section=$row->section;
	 $parent=$row->parent_id;
	 $ordering=$row->ordering;
	 }
	 ?>
 <table border="0" cellpadding="3" cellspacing="3" bordercolor="#CCCCCC">
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="hidden" name="act" value="product_cat_m">
<input type="hidden" name="id" value="<? echo $_REQUEST['id']; ?>">
<input type="hidden" name="page" value="<? echo $_REQUEST['page']; ?>">
    
	<tr>
      <td align="right" valign="middle">Title:</td>
      <td align="right" valign="middle"></td>
      <td align="left" valign="middle"><input name="title" type="text" value="<?=$title;?>" size="50"></td>
	</tr>
    
    
    <tr>
      <td align="right" valign="top">Parent:</td>
      <td valign="top" >&nbsp;</td>
      <td valign="top" >
	  <select size="1" name="parent">
        <?
		echo "<option value='0'>Root</option>";
		$cats=getListCat('','product_categories');
		foreach ($cats as $cat)
		{
			if ($cat[0]==$parent)
				echo "<option value=".$cat[0]." selected>".$cat[1]."</option>";
			else
				echo "<option value=".$cat[0].">".$cat[1]."</option>";
		}
		?>
      </select></td>
    </tr>	 
	<tr>
      <td align="right">Image:</td>
      <td >&nbsp;</td>
      <td ><input name="img" type="file" id="img" />
      <img src="../<?=$image;?>" border="0" /></td>
	</tr>
	<tr>
	  <td align="right">Published:</td>	  
	  <td >&nbsp;</td>
      <td ><input name="published" type="checkbox" id="published" value="1" checked="checked" /></td>
	</tr>

    <tr>
      <td>Ordering</td>
      <td>&nbsp;</td>
      <td><input name="ordering" type="text" size="5" value="<?=$ordering;?>" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><label></label></td>
      <td><input name="butSave" type="submit" id="butSave" value="Update" class="button" />        </td>
    </tr></form>
</table>

